package com.zaaach.citypicker.model;

import android.text.TextUtils;

import com.google.gson.annotations.SerializedName;
import com.zaaach.citypicker.util.PingyingUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * author Bro0cL on 2016/1/26.
 */
public class City {
    private String name;
    private String pinyin = "";
    @SerializedName("id")
    private String code;

    public City(String name, String pinyin, String code) {
        this.name = name;
        if (!TextUtils.isEmpty(pinyin)) {
            this.pinyin = pinyin;
        }else {
            if (!TextUtils.isEmpty(name)) {
                this.pinyin = PingyingUtils.getSpells(name);
            }
        }
        this.code = code;
    }

    /***
     * 获取悬浮栏文本，（#、定位、热门 需要特殊处理）
     * @return
     */
    public String getSection() {
        if (TextUtils.isEmpty(pinyin)) {
            return " #";
        } else {
            String c = pinyin.substring(0, 1);
            Pattern p = Pattern.compile("[a-zA-Z]");
            Matcher m = p.matcher(c);
            if (m.matches()) {
                return " "+c.toUpperCase();
            }
            //在添加定位和热门数据时设置的section就是‘定’、’热‘开头
            else if (TextUtils.equals(c, "定") || TextUtils.equals(c, "热"))
                return " "+pinyin;
            else
                return " #";
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        if (!TextUtils.isEmpty(name)) {
            this.pinyin = PingyingUtils.getSpells(name);
        }
    }

    public String getPinyin() {
        if (TextUtils.isEmpty(pinyin)){
            if (!TextUtils.isEmpty(name)) {
                this.pinyin = PingyingUtils.getSpells(name);
            }
        }
        return pinyin;
    }

    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
